//
//  NSDate+Custom.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "NSDate+Custom.h"

@implementation NSDate (Custom)
static NSDateFormatter* sharedDateFormatter() {
    static NSDateFormatter* sharedDateFormatter;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedDateFormatter = [[NSDateFormatter alloc] init];
        [sharedDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Harbin"]];
    });
    
    return sharedDateFormatter;
}

+ (id)dataFromString:(NSString *)string
{
    return [NSDate dataFromString:string withDateFormat:@"yyyyMMdd"];
}

+ (id)dataFromString:(NSString *)string withDateFormat:(NSString*)dataFormat
{
    [sharedDateFormatter() setDateFormat:dataFormat];
    NSDate *date = [sharedDateFormatter() dateFromString:string];
    
    return date;
}

+(NSString*) getWeekDayFromDate:(NSDate*) date
{
    
    //    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    //    [formatter setDateFormat:@"周c"];
    //    formatter
    //    NSString* strDate = [formatter stringFromDate:date];
    //    [formatter release];
    static   NSCalendar *calendar =nil;
    NSString* strWeekDate = nil;
    
    if(nil == calendar)
    {
        calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];
        [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Harbin"]];
    }
    NSDateComponents *comps =  nil;
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    comps = [calendar components:unitFlags fromDate:date];
    int week = (int)[comps weekday];
    switch (week) {
        case 0:
        {
            strWeekDate = @"";
        }
            break;
        case 1:
        {
            strWeekDate = @"日";
        }
            break;
        case 2:
        {
            strWeekDate = @"一";
        }
            break;
        case 3:
        {
            strWeekDate = @"二";
        }
            break;
        case 4:
        {
            strWeekDate = @"三";
        }
            break;
        case 5:
        {
            strWeekDate = @"四";
        }
            break;
        case 6:
        {
            strWeekDate = @"五";
        }
            break;
            
        case 7:
        {
            strWeekDate = @"六";
        }
            break;
            
        default:
            break;
    }
    
    
    
    return strWeekDate;
}

- (NSString*)formattedString
{
    [sharedDateFormatter() setDateFormat:@"yyyy/MM/dd"];
    return [sharedDateFormatter() stringFromDate:self];
}

- (NSString*)formattedStringWithFormat:(NSString*)format
{
    [sharedDateFormatter() setDateFormat:format];
    return [sharedDateFormatter() stringFromDate:self];
}

+(NSString*) nextDateStringFromDateString:(NSString*)  strCurDate
{
    NSDate*  date = [NSDate dataFromString:strCurDate];
    NSDate* tomorrow =[ NSDate dateWithTimeInterval:60*60*24 sinceDate:date];
    return [tomorrow formattedString];
}

+(NSString*) nextDateStringFromDate:(NSDate*) curDate
{
    NSDate* tomorrow =[ NSDate dateWithTimeInterval:60*60*24 sinceDate:curDate];
    return [tomorrow formattedString];
}


+(NSString*) preDateStringFromDateString:(NSString*)  strCurDate
{
    NSDate*  date = [NSDate dataFromString:strCurDate];
    NSDate* yestoday =[ NSDate dateWithTimeInterval:-60*60*24 sinceDate:date];
    return [yestoday formattedString];
}

+(NSString*) preDateStringFromDate:(NSDate*) curDate
{
    NSDate* yestoday =[ NSDate dateWithTimeInterval:-60*60*24 sinceDate:curDate];
    return [yestoday formattedString];
}

+ (NSString *)dateStringFromTimeInterval:(NSTimeInterval)timeInterval
{
    if (0 >= timeInterval)
        return @"";
    
    NSDate *nowDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *nowString = [dateFormatter stringFromDate:nowDate];
    NSDate *zeroDate = [dateFormatter dateFromString:nowString];
    
    // 发布点和零点均以当前时间为参考点，tencent:jiachunke(20140529)
    NSTimeInterval tempInterval = [nowDate timeIntervalSince1970] - timeInterval;
    NSTimeInterval zeroInterval = [nowDate timeIntervalSince1970] - [zeroDate timeIntervalSince1970];
    if (0 >= zeroInterval) {zeroInterval = DAY_TIME_INTERVAL;}
    
    if (MINUTE_TIME_INTERVAL > tempInterval) {
        return @"刚刚";
    }
    else if (HOUR_TIME_INTERVAL > tempInterval && tempInterval > MINUTE_TIME_INTERVAL) {
        NSUInteger minute = (NSUInteger)tempInterval/MINUTE_TIME_INTERVAL;
        return [NSString stringWithFormat:@"%lu分钟前", (unsigned long)minute];
    }
    else if (zeroInterval > tempInterval && tempInterval > HOUR_TIME_INTERVAL) {
        NSUInteger hour = (NSUInteger)tempInterval/HOUR_TIME_INTERVAL;
        return [NSString stringWithFormat:@"%lu小时前", (unsigned long)hour];
    }
    else if ((zeroInterval+DAY_TIME_INTERVAL) > tempInterval && tempInterval > zeroInterval) {
        return [NSString stringWithFormat:@"昨天"];
    }
    else if ((zeroInterval+WEEK_TIME_INTERVAL) > tempInterval && tempInterval > zeroInterval) {
        NSUInteger day = (NSUInteger)tempInterval/DAY_TIME_INTERVAL;
        return [NSString stringWithFormat:@"%lu天前", (unsigned long)day];
    }
    
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSString *dateString = [dateFormatter stringFromDate:date];
    
    return dateString;
}


+ (NSUInteger) secendValueFromString:(NSString*)videoDuration
{
    NSArray * array = [videoDuration componentsSeparatedByString:@":"];
    NSUInteger secend = 0;
    if (array.count == 2) {
        secend = [array[0] integerValue] * 60 +  [array[1] integerValue];
    }
    else if(array.count == 1)
    {
        secend = [array[0] integerValue];
    }
    return secend;
}

@end
